var jwxaSwiper = {},
    // Swiper组件默认配置
    __swiperSettings = {
        autoplay: true,
        interval: 5000,
        duration: 500,
        items: [],
        dotsClass: [],
    },
    __navigatorSettings = {
        openType: 'navigate',
        hoverClass: 'navigator-hover'
    };

/*
 初始化swiper组件
 jwxa : 依赖的jwxa.js对象
 options : 配置项
 - autoplay : 是否自动切换
 - interval : 自动切换时间间隔
 - duration : 滑动动画时长
 - items : 图片项
 --- navigator : 图片是否需要调整导航
 ----- url : 应用内的跳转链接
 ----- open-type : 可选值 'navigate'、'redirect'、'switchTab'
 ----- hover-class : 指定点击时的样式类，当hover-class="none"时，没有点击态效果
 --- imgUrl : 图片URL地址
 - dotsClass : swiper组件的dot(内部属性,请不要改动)
 bindchange : current 改变时会触发 change 事件
 */
jwxaSwiper.initSwiper = function(jwxa, options, bindchange) {
    this.__jwxa = jwxa;
    jwxa.isFunction(options) && (bindchange = options, options = {});
    var settings = options || {};
    for (var key in __swiperSettings) {
        if (settings[key] === undefined) settings[key] = __swiperSettings[key];
    };
    // 给Page对象添加Swiper改变事件
    jwxa.__target.jwxaSwiperChange = function (event) {
        var dotsClass = this.data.jwxaSwiper.dotsClass;
        for (var i in dotsClass) {
            dotsClass[i] = i == event.detail.current ? 'on' : '';
        };
        jwxaSwiper.__jwxa.data('jwxaSwiper.dotsClass', dotsClass);
        bindchange && bindchange.call(this, event);
    };
    jwxa.data({jwxaSwiper: settings});
};
// 设置items
jwxaSwiper.items = function (items) {
    var itemSettings = items || {};
    var jwxaSwiper = this.__jwxa.__target.data.jwxaSwiper;
    jwxaSwiper.items = itemSettings;
    for (var i in itemSettings) {
        jwxaSwiper.dotsClass[i] = i == 0 ? 'on' : '';
        var navigator = itemSettings[i].navigator;
        navigator && ((navigator.openType) || (navigator.openType = __navigatorSettings.openType));
        navigator && ((navigator.hoverClass) || (navigator.hoverClass = __navigatorSettings.hoverClass));
    };
    this.__jwxa.data({jwxaSwiper: jwxaSwiper});
}

module.exports = jwxaSwiper;